package util;

import java.io.IOException;
import java.io.InputStream;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;
import net.htmlparser.jericho.TextExtractor;


/**
 * @author Guimin Lin
 * @date Feb 1, 2011
 */
public class HtmlContentExtractor {
	private StringBuffer DocBuffer;//title and body content of html
	
	public boolean parseDoc(InputStream inputStream) {
		DocBuffer=new StringBuffer();
		boolean isSucceed=false;//indicate whether successfully parse the document
		Source source=null;
		Element titleElement,bodyElement;
		TextExtractor titleExtractor,bodyExtractor;
		
		try {
			 source=new Source(inputStream);//parse the document
			 source.setLogger(null);//turn off error and warning output
			 
		} catch (IOException e) {
			e.printStackTrace();
			return isSucceed;
		}
		if (source!=null) {
			titleElement = source.getFirstElement(HTMLElementName.TITLE);
			bodyElement = source.getFirstElement(HTMLElementName.BODY);
			
			//extract text without html tag
			if (titleElement != null) {
				titleExtractor = titleElement.getTextExtractor();
				if (titleExtractor != null) {
					titleExtractor = titleExtractor
							.setExcludeNonHTMLElements(true);
					if (titleExtractor != null) {
						DocBuffer.append(titleExtractor.toString());
					}

				}
			}
			
			//extract text without html tag
			if (bodyElement!=null) {
				bodyExtractor = bodyElement.getTextExtractor();
				if (bodyExtractor != null) {
					bodyExtractor = bodyExtractor
							.setExcludeNonHTMLElements(true);
					if (bodyExtractor != null) {
						DocBuffer.append(bodyExtractor.toString());
					}
				}
			}
			
			isSucceed = true;
		}
		
		return isSucceed;
		
	}

	/**
	 * @return the docBuffer
	 */
	public StringBuffer getDocBuffer() {
		return DocBuffer;
	}
	
	
	
}
